﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Phone.Shell;

namespace sendNotifications
{
    public class InstantLiveTileUpdateHelper
    {
        public void CreateLiveTile(Uri frontImage, string Title, int Count, string BackTitle, string BackContent, Uri backImage)
        {
            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileID=MockingbirdIM"));

            // Create the tile if we didn't find it already exists.
            if (TileToFind == null)
            {
                // Create the tile object and set the properties for the tile.
                StandardTileData NewTileData = new StandardTileData
                {
                    BackgroundImage = frontImage,
                    Title = Title,
                    Count = Count,
                    BackTitle = BackTitle,
                    BackContent = BackContent,
                    BackBackgroundImage = backImage
                };

                // Create the tile and pin it to Start. This will cause a navigation to Start and a deactivation of our application.
                ShellTile.Create(new Uri("/MainPage.xaml?TileID=MockingbirdIM", UriKind.Relative), NewTileData);
            }
        }

        public void UpdateLiveTile(Uri frontImage, string Title, int Count, string BackTitle, string BackContent, Uri backImage)
        {
            // Find the tile we want to update.
            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileID=MockingbirdIM"));


            // If tile was found, then update the Title
            if (TileToFind != null)
            {
                StandardTileData NewTileData = new StandardTileData
                {
                    BackgroundImage = frontImage,
                    Title = Title,
                    Count = Count,
                    BackTitle = BackTitle,
                    BackContent = BackContent,
                    BackBackgroundImage = backImage
                };

                TileToFind.Update(NewTileData);
            }

        }

        public void DeleteLiveTile()
        {
            // Find the tile we want to delete.
            ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileID=MockingbirdIM"));

            // If tile was found, then delete it.
            if (TileToFind != null)
            {
                TileToFind.Delete();
            }
        }

        public void Toast(string Title, string Content, Uri navUri)
        {
            ShellToast toast = new ShellToast();

            toast.Title = Title;

            toast.Content = Content;

            toast.NavigationUri = navUri;

            toast.Show();
        }
    }
}
